<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        .gradient-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }

        .input-focus:focus {
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.3);
        }

        .shake {
            animation: shake 0.5s;
        }

        @keyframes shake {
            0%, 100% {
                transform: translateX(0);
            }
            20%, 60% {
                transform: translateX(-5px);
            }
            40%, 80% {
                transform: translateX(5px);
            }
        }
    </style>
</head>
<body class="min-h-screen flex items-center justify-center gradient-bg">
<div class="w-full max-w-md px-8 py-12 bg-white rounded-2xl shadow-xl" id="app">
    <div class="text-center mb-10">
        <i class="fas fa-user-circle text-6xl text-indigo-500 mb-4"></i>
        <h1 class="text-3xl font-bold text-gray-800">欢迎回来</h1>
        <p class="text-gray-500 mt-2">{{error}}</p>
    </div>

    <div>
        <label for="email" class="block text-sm font-medium text-gray-700 mb-1">账号</label>
        <div class="relative">
            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                <i class="fas fa-envelope text-gray-400"></i>
            </div>
            <input id="email" type="text" required v-model="username"
                   class="block w-full pl-10 pr-3 py-3 border border-gray-300 rounded-lg input-focus transition duration-200"
                   placeholder="example@domain.com">
        </div>
        <p id="emailError" class="mt-1 text-sm text-red-600 hidden">请输入有效的邮箱地址</p>
    </div>

    <div>
        <label for="password" class="block text-sm font-medium text-gray-700 mb-1">密码</label>
        <div class="relative">
            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                <i class="fas fa-lock text-gray-400"></i>
            </div>
            <input id="password" name="password" type="password" required minlength="6"
                   v-model="password"
                   class="block w-full pl-10 pr-3 py-3 border border-gray-300 rounded-lg input-focus transition duration-200"
                   placeholder="至少6位字符">
            <button type="button" id="togglePassword" class="absolute inset-y-0 right-0 pr-3 flex items-center">
                <i class="fas fa-eye text-gray-400 hover:text-indigo-500"></i>
            </button>
        </div>
        <p id="passwordError" class="mt-1 text-sm text-red-600 hidden">密码长度至少6位</p>
    </div>

    <div class="flex items-center justify-between">
        <div class="flex items-center">
            <input id="remember" name="remember" type="checkbox"
                   class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded">
            <label for="remember" class="ml-2 block text-sm text-gray-700">记住我</label>
        </div>
        <a href="#" class="text-sm text-indigo-600 hover:text-indigo-500">忘记密码?</a>
    </div>

    <button type="submit" @click="login"
            class="w-full flex justify-center py-3 px-4 border border-transparent rounded-lg shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition duration-200">
        登录
    </button>

    <div class="mt-6 text-center">
        <p class="text-sm text-gray-600">
            还没有账号?
            <a href="#" class="font-medium text-indigo-600 hover:text-indigo-500">立即注册</a>
        </p>
    </div>
</div>

</body>
</html>
<script src="../js/vue.min.js"></script>
<script src="../js/axios.min.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                username: "",
                password: "",
                error: ''
            }
        },
        methods: {
            login() {
                let data = new URLSearchParams()
                data.append("username", this.username)
                data.append("password", this.password)
                axios({
                    method: 'post',
                    url: 'http://localhost:8080/auth/login',
                    data: data
                }).then(resp => {
                    if (resp.data.success) {
                        // 1.保存token
                        localStorage.setItem('token', resp.data.data)
                        // 2.跳转到首页中
                        location.href = 'index.html'
                    } else {
                        // 1.提示登录失败
                        this.error = '账号或者密码错误'
                    }
                })
            }
        }
    })
</script>
